/*
 * Copyright 2009 Sven Meier
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.malex.mxtracker.component.content;

import com.malex.mxtracker.bean.Project;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.model.IModel;
import wickettree.AbstractTree;
import wickettree.ITreeProvider;
import wickettree.content.CheckedFolder;
import wickettree.provider.ProviderSubset;


/**
 * @author Sven Meier
 */
public class CheckedFolderContent extends Content {

    private static final long serialVersionUID = 1L;

    private ProviderSubset<Project> checked;

    public CheckedFolderContent(ITreeProvider<Project> provider) {
        checked = new ProviderSubset<Project>(provider, false);
    }

    public void detach() {
        checked.detach();
    }

    protected boolean isChecked(Project foo) {
        return checked.contains(foo);
    }

    protected void check(Project foo, boolean check, final AjaxRequestTarget target) {
        if (check) {
            checked.add(foo);
        } else {
            checked.remove(foo);
        }
    }

    public Component newContentComponent(String id, final AbstractTree<Project> tree, IModel<Project> model) {
        return new CheckedFolder<Project>(id, tree, model) {
            private static final long serialVersionUID = 1L;

            @Override
            protected IModel<Boolean> newCheckBoxModel(final IModel<Project> model) {
                return new IModel<Boolean>() {
                    private static final long serialVersionUID = 1L;

                    public Boolean getObject() {
                        return isChecked(model.getObject());
                    }

                    public void setObject(Boolean object) {
                        check(model.getObject(), object, AjaxRequestTarget.get());
                    }

                    public void detach() {
                    }
                };
            }
        };
    }
}